﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.Framework.Common.Security.Services.Authentication.ClientCredentials;

namespace PSE.Framework.UI.Security.Authentication
{
    public class AuthenticationContext
    {
        private static object _syncRoot = new object();
        private static AuthenticationContext _instance;

        private Dictionary<string, object> ContextData
        {
            get;
            set;
        }

        public CustomClientCredentials ClientCredentials
        {
            get
            {
                return AuthenticationManager.GetInstance().ClientCredentials;
            }
        }

        public static AuthenticationContext Current
        {
            get
            {
                lock (_syncRoot)
                {
                    if (_instance == null)
                        _instance = new AuthenticationContext();
                }

                return _instance;
            }
        }


        private AuthenticationContext()
        {
            this.ContextData = new Dictionary<string, object>();
        }

        /// <summary>
        /// Armazenar dados no contexto de autenticação
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void StoreData(string key, object value)
        {
            lock (this.ContextData)
            {
                if (!this.ContextData.ContainsKey(key))
                    this.ContextData.Add(key, value);
                else
                    this.ContextData[key] = value;
            }
        }

        /// <summary>
        /// Remover dados armazenados no contexto de autenticação
        /// </summary>
        /// <param name="key"></param>
        public void RemoveData(string key)
        {
            lock (this.ContextData)
            {
                if (this.ContextData.ContainsKey(key))
                    this.ContextData.Remove(key);
            }
        }

        /// <summary>
        /// Recuperar dados armazenados no contexto de autenticação
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object GetData(string key)
        {
            lock (this.ContextData)
            {
                if (this.ContextData.ContainsKey(key))
                    return this.ContextData[key];
            }

            return null;
        }

        /// <summary>
        /// Verficar se existe dados amarzenados para a key informada
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsData(string key)
        {
            return this.ContextData.ContainsKey(key);
        }

    }
}
